package cn.jinzhu.zuul;

import com.netflix.zuul.ZuulFilter;
import com.netflix.zuul.context.RequestContext;
import com.netflix.zuul.exception.ZuulException;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
@Component
//zuul过滤器
public class PermissFilter extends ZuulFilter {
    /*
    * 过滤器的类型，权限判断的方法,一般是pre
    * */
    @Override
    public String filterType() {
        return "pre";
    }
  //过滤器优先级
    @Override
    public int filterOrder() {
        return 0;
    }
  //是否走过滤器,默认不走
    @Override
    public boolean shouldFilter() {
        return true;
    }
  //过滤器的逻辑写在这里,过滤器判断登录是否满足条件，不满足则没有权限进不去接口
    @Override
    public Object run() throws ZuulException {
        RequestContext context = RequestContext.getCurrentContext();
        HttpServletRequest request = context.getRequest();
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        if(!"jinzhu".equals(username) || !"123".equals(password) ){
                 context.setSendZuulResponse(false);
                 context.setResponseStatusCode(401);
                 context.addZuulResponseHeader("content-type","text/html;charset=utf-8");
                 context.setResponseBody("非法访问");
        }
        return null;
    }
}
